﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProductReleasing : System.Web.UI.Page
{
    MyDataDataContext db = new MyDataDataContext();
    string str = System.Guid.NewGuid().ToString("N");//构造唯一一个标识码
    protected void Page_Load(object sender, EventArgs e)
    {
        //页面首次加载
        if (!IsPostBack)
        {
            //获取商品分类信息
            var results = from r in db.GoodsType
                          select new
                          {
                              r.GoodsTypeName
                          };//映射需要显示的字段

            //设置DropDownList控件的数据源
            Type.DataSource = results;
            //调用DataBind()方法显示数据
            Type.DataBind();
        }
    }

    //删除商品
    protected void DeleteButton_Click(object sender, EventArgs e)
    {
        //不需要检查控件验证是否通过
        //获取要删除的对象
        MyDataDataContext db = new MyDataDataContext();
        LookingOfGoods DeleteObj = (from r in db.LookingOfGoods
                                    where r.GoodsID == Request.QueryString["GoodsID"]
                                    select r).FirstOrDefault();

        //删除
        db.LookingOfGoods.DeleteOnSubmit(DeleteObj);
        db.SubmitChanges();

        //返回信息列表页面
        Response.Redirect("ChangeInformation2.aspx");
    }

    //发布物品信息
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        if (Session["UserId"] == null)//用户未登录，返回登录界面
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            //控件全部通过验证
            if (Page.IsValid)
            {
                //添加发布物品记录
                GoodsType typeID = (from r in db.GoodsType
                                    where r.GoodsTypeName == Type.SelectedValue
                                    select r).First();


                string LsTime = DateTime.Now.ToString();


                LookingOfGoods goods = new LookingOfGoods();
                goods.GoodsID = str;
                //商品名称
                goods.GoodsName = GoodsName.Text.Trim();
                //商品描述
                goods.GoodsDescription = GoodsDescription.Text.Trim();
                //商品价格
                goods.PriceCap = double.Parse(PriceCap.Text.Trim());//物品价格上限
                goods.PriceFloor = double.Parse(PriceFloor.Text.Trim());//物品价格下限
                goods.ReleaseTime = LsTime;
                goods.DeleteTime = LsTime;//物品删除时间
                goods.StateOfGoods = 1;//1表示物品处于正在进行中，0表示物品下架/被删除
                goods.UserID = Session["UserId"].ToString(); //发布者姓名             

                //商品类型
                //商品类型ID
                goods.GoodsTypeID = typeID.GoodsTypeID;
                goods.Type = typeID.GoodsTypeName;
                goods.GoodsOrigin = 1;//1表示商品来自于出售物品，2表示物品来自于想买物品

                //提交
                db.LookingOfGoods.InsertOnSubmit(goods);
                db.SubmitChanges();
                lblMsg.Text = "发布成功";
                //返回信息列表页面
                //Response.Redirect("ProductReleasing.aspx");
            }
        }
    }
}